package com.ovopark.organize.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateFieldMapper;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateFieldSelectFillMapper;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateFieldSelectMapper;
import com.ovopark.organize.server.mapper.FranchiseesApprovalTemplateSectionMapper;
import com.ovopark.organize.server.model.entity.*;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateFieldService;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateSectionService;
import com.ovopark.organize.server.service.FranchiseesApprovalTemplateService;
import com.ovopark.organize.server.util.CollectionUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wxb
 * @Title: FranchiseesApprovelServiceImpl
 * @Description: 加盟商立项模板分组
 * @date 2024/4/11 15:51
 **/
@Service
@Slf4j
public class FranchiseesApprovalTemplateSectionServiceImpl extends ServiceImpl<FranchiseesApprovalTemplateSectionMapper, FranchiseesApprovalTemplateSection> implements FranchiseesApprovalTemplateSectionService {

    @Resource
    private FranchiseesApprovalTemplateFieldService franchiseesApprovalTemplateFieldService;

    @Resource
    private FranchiseesApprovalTemplateService franchiseesApprovalTemplateService;

    @Resource
    private FranchiseesApprovalTemplateSectionMapper franchiseesApprovalTemplateSectionMapper;

    @Resource
    private FranchiseesApprovalTemplateFieldMapper franchiseesApprovalTemplateFieldMapper;

    @Resource
    private FranchiseesApprovalTemplateFieldSelectMapper franchiseesApprovalTemplateFieldSelectMapper;

    @Resource
    private FranchiseesApprovalTemplateFieldSelectFillMapper franchiseesApprovalTemplateFieldSelectFillMapper;


    /**
     * @description: 获取模板下的分组
     * @param: [templateId, version]
     * @return: java.util.List<com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateSection>
     * @author wxb
     * @date: 2024/6/1 12:45
     */
    @Override
    public List<FranchiseesApprovalTemplateSection> getSectionListByTemplateId(FranchiseesApprovalTemplate template, String version) {
        List<FranchiseesApprovalTemplateSection> list = new ArrayList<>();
        // 模板ID
        Integer templateId = template.getId();
        // 企业ID
        Integer groupId = template.getGroupId();
        if (templateId != null) {
            LambdaQueryWrapper<FranchiseesApprovalTemplateSection> queryWrapper = new LambdaQueryWrapper<>();
            // 企业自己的模板
            if (groupId != null) {
                queryWrapper.eq(FranchiseesApprovalTemplateSection::getGroupId, groupId);
            } else {
                // 系统模板
                queryWrapper.isNull(FranchiseesApprovalTemplateSection::getGroupId);
            }
            queryWrapper.eq(FranchiseesApprovalTemplateSection::getTemplateId, template.getId());
            queryWrapper.eq(FranchiseesApprovalTemplateSection::getIsDeleted, 0);
            // 企业子的模板才去查询版本号
            if (groupId != null) {
                if (StringUtils.isNotEmpty(version)) {
                    queryWrapper.eq(FranchiseesApprovalTemplateSection::getVersion, version);
                } else {
                    // 获取版本号
                    List<String> versionList = franchiseesApprovalTemplateService.getVersionListByTemplateId(templateId, null, groupId);
                    if (CollectionUtil.isNotEmpty(versionList)) {
                        queryWrapper.eq(FranchiseesApprovalTemplateSection::getVersion, versionList.get(0));
                    }
                }
            }
            queryWrapper.orderByAsc(FranchiseesApprovalTemplateSection::getSort);
            List<FranchiseesApprovalTemplateSection> sections = this.baseMapper.selectList(queryWrapper);
            if (CollectionUtil.isNotEmpty(sections)) {
                sections.forEach(section -> {
                    // 获取字段
                    List<FranchiseesApprovalTemplateField> fields = franchiseesApprovalTemplateFieldService.getFieldListBySectionId(section.getId());
                    section.setFields(fields);
                    list.add(section);
                });
            }
        }
        return list;
    }

    @Override
    public void batchSaveSectionList(List<FranchiseesApprovalTemplateSection> sectionList) {
        if (CollectionUtil.isNotEmpty(sectionList)) {
            sectionList.forEach(section -> {
                FranchiseesApprovalTemplateSection newSection = this.saveOrUpdateById(section);
                // 保存字段
                List<FranchiseesApprovalTemplateField> fields = newSection.getFields();
                if (CollectionUtil.isNotEmpty(fields)) {
                    fields.forEach(field -> {
                        field.setSectionId(newSection.getId());
                    });
                    franchiseesApprovalTemplateFieldService.batchSaveFieldList(fields);
                }
            });
        }
    }

    @Override
    public FranchiseesApprovalTemplateSection saveOrUpdateById(FranchiseesApprovalTemplateSection section) {
        if (section != null) {
            if (section.getId() != null) {
                // 更新
                baseMapper.updateById(section);
            } else {
                // 新增
                section.setCreateTime(LocalDateTime.now());
                baseMapper.insert(section);
            }
            return section;
        }
        return null;
    }

    /**
     * @description: 保存配置
     * @param: [sectionList]
     * @return: java.util.List<com.ovopark.organize.server.model.entity.FranchiseesApprovalTemplateSection>
     * @author wxb
     * @date: 2024/5/31 21:22
     */
    @Override
    public void insert(Integer groupId, Integer templateId, List<FranchiseesApprovalTemplateSection> sectionList, Integer userId) {
        if (CollectionUtils.isEmpty(sectionList)){
            return;
        }
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        // 版本号
        String version = LocalDateTime.now().format(formatter);
        for (FranchiseesApprovalTemplateSection section : sectionList) {
            section.setGroupId(groupId);
            section.setTemplateId(templateId);
            section.setVersion(version);
            section.setUserId(userId);
            section.setCreateTime(LocalDateTime.now());
            franchiseesApprovalTemplateSectionMapper.insert(section);
            // 题目设置
            List<FranchiseesApprovalTemplateField> fields = section.getFields();
            if (CollectionUtil.isNotEmpty(fields)) {
                for (FranchiseesApprovalTemplateField field : fields) {
                    field.setSectionId(section.getId());
                    field.setVersion(version);
                    franchiseesApprovalTemplateFieldMapper.insert(field);
                    // 选项设置
                    List<FranchiseesApprovalTemplateFieldSelect> fieldSelectList = field.getFieldSelectList();
                    if (CollectionUtil.isNotEmpty(fieldSelectList)) {
                        for (FranchiseesApprovalTemplateFieldSelect fieldSelect : fieldSelectList) {
                            fieldSelect.setFieldId(field.getId());
                            franchiseesApprovalTemplateFieldSelectMapper.insert(fieldSelect);
                            // 填空
                            List<FranchiseesApprovalTemplateFieldSelectFill> fieldSelectFillList = fieldSelect.getResource();
                            if (CollectionUtil.isNotEmpty(fieldSelectFillList)) {
                                for (FranchiseesApprovalTemplateFieldSelectFill fieldSelectFill : fieldSelectFillList) {
                                    fieldSelectFill.setSelectId(fieldSelect.getId());
                                    franchiseesApprovalTemplateFieldSelectFillMapper.insert(fieldSelectFill);
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}
